package pl.rzarajczyk.breaktime;



import java.io.File;
import java.io.IOException;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pl.rzarajczyk.breaktime.utils.Settings;
import pl.rzarajczyk.breaktime.utils.SettingsEvent;
import pl.rzarajczyk.breaktime.utils.Utils;

/**
 *
 * @author rzarajczyk
 */
public class BreakTimeSettings extends Settings {
    
    public static final boolean LOCALIZATION_COLLECT_TEXTS = true;
    public static final String LOCALIZATION_EXT = ".properties";
    public static int TRAY_INITIALIZATION_COUNT = 3;
    public static int TRAY_INITIALIZATION_INTERVAL = 1000;
    
    private int microBreakInterval;
    private int microBreakDuration;
    boolean showControlls;
    private Locale locale;
    
    private Log log = LogFactory.getLog(getClass());
    
    private static File getPropertiesFile() throws IOException {
        File dir = Utils.getSettingsDir();
        return new File(dir, "settings.xml");
    }

    public BreakTimeSettings() throws IOException {
        this( getPropertiesFile() );
    }

    public BreakTimeSettings(File file) throws IOException {
        super(file, false);
        this.addEvent( new SettingsEvent() {

            @Override
            public void onAfterLoad() {
                microBreakInterval = get("micro-break-interval", 15 * 60 * Utils.SECOND);
                microBreakDuration = get("micro-break-duration", 10 * Utils.SECOND);
                showControlls = get("show-controlls", true);
                locale = get("locale", Locale.class, null);
                if ( microBreakInterval <= 1 ) {
                    log.warn("Invalid micro break interval (" + microBreakInterval + "); resetting to 30");
                    microBreakInterval = 30;
                }
                if ( microBreakDuration <= 1 ) {
                    log.warn("Invalid micro break duration (" + microBreakDuration + "); resetting to 10");
                    microBreakDuration = 10;
                }
            }

            @Override
            public void onBeforeSave() {
                beginBatchUpdate();
                set("micro-break-interval", microBreakInterval);
                set("micro-break-duration", microBreakDuration);
                set("show-controlls", showControlls);
                set("locale", locale);
                finishBatchUpdate();
            }

            @Override
            public void onUpdate() {
                // nothing
            }
        } );
    }
    
    // =========================================================================

    public int getMicroBreakInterval() {
        return microBreakInterval;
    }

    public int getMicroBreakDuration() {
        return microBreakDuration;
    }

    public void setMicroBreakInterval(int value) {
        this.microBreakInterval = value;
    }

    public void setMicroBreakDuration(int value) {
        this.microBreakDuration = value;
    }

    public void setShowControlls(boolean value) {
        this.showControlls = value;
    }

    public boolean isShowControlls() {
        return showControlls;
    }

    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }
    
    public String getVersion() {
        return getMetaProperty("version", "UNKNOWN");
    }

}
